package com.ikun.mallbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ikun.mallbackend.entity.BigType;
import com.ikun.mallbackend.entity.Product;
import com.ikun.mallbackend.entity.Result;
import com.ikun.mallbackend.entity.SmallType;
import com.ikun.mallbackend.service.IBigTypeService;
import com.ikun.mallbackend.service.IProductService;
import com.ikun.mallbackend.service.ISmallTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品大类Controller
 * @author java1234_小锋
 * @site www.java1234.com
 * @company 南通小锋网络科技有限公司
 * @create 2022-02-23 22:03
 */
@RestController
@RequestMapping("/bigType")
public class BigTypeController {

    private final IBigTypeService bigTypeService;

    public BigTypeController(IBigTypeService bigTypeService, ISmallTypeService smallTypeService, IProductService productService) {
        this.bigTypeService = bigTypeService;
        this.smallTypeService = smallTypeService;
        this.productService = productService;
    }

    private final ISmallTypeService smallTypeService;
    private final IProductService productService;



    /**
     * 查询所有商品大类
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<BigType> bigTypeList = bigTypeService.list();
        Map<String,Object> map=new HashMap<>();
        map.put("message",bigTypeList);
        return Result.ok(map);
    }

    /**
     * 以分类的形式获取所有的product
     * @return
     */
    @GetMapping("/findCategories")
    public Result findCategory(){
        QueryWrapper<BigType> bigTypeQueryWrapper = new QueryWrapper<>();
        List<BigType> bigTypeList = bigTypeService.list(bigTypeQueryWrapper);
        for (BigType bigType : bigTypeList) {
            QueryWrapper<SmallType> smallTypeQueryWrapper = new QueryWrapper<SmallType>().eq("bigTypeId", bigType.getId());
            List<SmallType> smallTypeList =smallTypeService.list(smallTypeQueryWrapper);
            bigType.setSmallTypeList(smallTypeList);
            for (SmallType smallType : smallTypeList) {
                List<Product> productList = productService.list(new QueryWrapper<Product>().eq("typeId", smallType.getId()));
                smallType.setProductList(productList);
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("message",bigTypeList);
        return Result.ok(map);
    }
}
